import {ref} from 'vue';
import apiParts from '@/util/network_api/parts.js';

export const useStockCheckBeforeSparePartApplyFormSubmitService = () => {
	const stockCheckForm = ref<{
		networkId: string;
		materialQty: Record<string, number>;
	}>({
		networkId: '',
		materialQty: {},
	});

	const stockCheckResult = ref<{isSufficient: 0 | 1; type: 1 | 2 | 3; difference: string}>({
		isSufficient: 0,
		type: 1,
		difference: "",
	});

	const stockCheck = async () => {
		try {
			const res = await apiParts.checkStockBeforeSparePartApplySubmit(
				stockCheckForm.value,
			);
			if (res.code === 20000) {
				stockCheckResult.value = res.data.row;
			}
		} catch (e) {
			return '校验库存失败，请稍后重试';
		}
	};

	return {
		stockCheckForm,
		stockCheckResult,
		stockCheck,
	};
};
